<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="中文简称" :label-width="formLabelWidth" prop="countryNameCn">
				<el-input v-model="form.countryNameCn" autocomplete="off" placeholder="请输入中文简称"></el-input>
			</el-form-item>
			<el-form-item label="英文简称" :label-width="formLabelWidth" prop="countryNameEn">
				<el-input v-model="form.countryNameEn" autocomplete="off" placeholder="请输入英文简称"></el-input>
			</el-form-item>
			<el-form-item label="英文全称" :label-width="formLabelWidth" prop="countryNameEnFull">
				<el-input v-model="form.countryNameEnFull" autocomplete="off" placeholder="请输入英文全称"></el-input>
			</el-form-item>
			<el-form-item label="国旗图片" :label-width="formLabelWidth" prop="nationalFlag">
				<!-- <fortress-upload type="image" name="nationalFlag" :formData="form"></fortress-upload> -->
				<picture-upload type="picture-card" v-model="form.nationalFlag" :limit="1"></picture-upload>
			</el-form-item>
			<el-form-item label="两字母代码" :label-width="formLabelWidth" prop="alphaCode2">
				<el-input v-model="form.alphaCode2" autocomplete="off" placeholder="请输入两字母代码"></el-input>
			</el-form-item>
			<el-form-item label="三字母代码" :label-width="formLabelWidth" prop="alphaCode3">
				<el-input v-model="form.alphaCode3" autocomplete="off" placeholder="请输入三字母代码"></el-input>
			</el-form-item>
			<el-form-item label="数字代码" :label-width="formLabelWidth" prop="countryCode">
				<el-input v-model="form.countryCode" autocomplete="off" placeholder="请输入数字代码"></el-input>
			</el-form-item>
			<el-form-item label="区号" :label-width="formLabelWidth" prop="areaCode">
				<el-input v-model="form.areaCode" autocomplete="off" placeholder="请输入区号"></el-input>
			</el-form-item>
			<el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
				<el-input type="textarea" :rows="2" placeholder="请输入备注" v-model="form.remark"></el-input>
			</el-form-item>
			<el-form-item label="中文拼音" :label-width="formLabelWidth" prop="chinesePinyin">
				<el-input v-model="form.chinesePinyin" autocomplete="off" placeholder="请输入中文拼音"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
			<el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>
<script>
import { syscountryInfo, syscountrySave, syscountryEdit } from "@/api/system/syscountry.js"
import pictureUpload from "@/components/fortress-upload/picture-upload.vue"

export default {
	data() {
		return {
			visible: false,
			form: {
				countryNameCn: "",
				countryNameEn: "",
				countryNameEnFull: "",
				nationalFlag: "",
				alphaCode2: "",
				alphaCode3: "",
				countryCode: "",
				areaCode: "",
				remark: "",
				chinesePinyin: "",
			},
			formLabelWidth: "120px",
			rules: {},
		}
	},
	created() {},
	methods: {
		init(id) {
			this.resetForm()
			//修改
			if (id) {
				syscountryInfo(id).then(resp => {
					if (resp.code == 0) {
						this.$nextTick(() => {
							this.form = {
								...this.form,
								...resp.data,
							}
						})
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
			}
			this.visible = true
		},
		//提交表单数据
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					//修改
					if (this.form.id) {
						syscountryEdit(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
						//新增
					} else {
						syscountrySave(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					}
				} else {
					console.log("error submit!!")
					return false
				}
			})
		},
		resetForm() {
			this.form = {
				countryNameCn: "",
				countryNameEn: "",
				countryNameEnFull: "",
				nationalFlag: "",
				alphaCode2: "",
				alphaCode3: "",
				countryCode: "",
				areaCode: "",
				remark: "",
				chinesePinyin: "",
			}
			this.$nextTick(() => {
				if (this.$refs["form"]) {
					this.$refs["form"].resetFields()
				}
			})
		},
	},
	components: {
		pictureUpload,
	},
}
</script>
<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
	height: auto;
	max-height: 274px;
	padding: 7px;
	overflow: hidden;
	overflow-y: auto;
	font-weight: normal;
}
</style>
